<?php

require_once 'Zend/Form/Element/Text.php';

class ZFKiller_Form_Element_Date extends Zend_Form_Element_Text {

	public function __construct($spec, $options = null) {
		ZFKiller_Form_Element::formatDefaultOptions($spec, $options);
		parent::__construct($spec, $options);

		ZFKiller_Form_Element::formatElementAsDate($this);
		$this->addValidators(array('Date'));
	}

	public function getValue() {
		switch (ZFKiller_Form::getValueType()) {
			case ZFKiller_Form::GET_VALUE_TYPE_DB:
				return $this->getValueDb();
				break;
			case ZFKiller_Form::GET_VALUE_TYPE_USER:
				return $this->getValueUser();
				break;
			default:
				return $this->getValueUser();
				//return $this->getUnfilteredValue();
				break;
		}
	}

	public function getValueDb() {
		$date = $this->getUnfilteredValue();
		if (!$date instanceof Zend_Date) {
			$date = new Zend_Date($this->getUnfilteredValue(), Zend_Locale_Format::getDateFormat());
		}

		return $date->getIso();
	}

	public function getValueUser() {
		$date = $this->getUnfilteredValue();
		if (!$date instanceof Zend_Date) {
			$date = new Zend_Date($this->getUnfilteredValue(), Zend_Locale_Format::getDateFormat());
		}
		return $date->get(Zend_Date::DATES);
	}

	public function setValue($date) {
		if (Zend_Date::isDate($date)) {
			$date = new Zend_Date($date, Zend_Locale_Format::getDateFormat());
		}
		return parent::setValue($date);
	}

}